package com.ygy.jxc.controller;

import com.ygy.jxc.model.JxmdPurchase;
import com.ygy.jxc.model.JxmdSale;
import com.ygy.jxc.service.JxSaleService;
import com.ygy.jxc.util.ResultCode;
import com.ygy.jxc.util.commonPage;
import com.ygy.jxc.util.commonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@Api(tags = "JxSaleController" , description = "jx-3销售表")
@Tag(name = "JxSaleController", description = "进销管理-销售表")
@RequestMapping("/sale")
public class JxSaleController {

    @Autowired
    private JxSaleService saleService;


    @ApiOperation("添加 ")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public commonResult create(@RequestBody JxmdSale sale) {
        int count = saleService.addOrUpdate(sale);
        if (count > 0) {
            return commonResult.success(count);
        }
        return commonResult.failed();
    }

    @ApiOperation("修改 ")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public commonResult update( @RequestBody  JxmdSale sale) {
        int count = saleService.update( sale);
        if (count > 0) {
            return commonResult.success(count);
        }
        return commonResult.failed();
    }

    @ApiOperation("批量删除 ")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public commonResult delete(Long id) {
        int count = saleService.delete(id);
        if (count > 0) {
            return commonResult.success(count);
        }
        return commonResult.failed();
    }

    @ApiOperation("获取所有 ")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public commonResult<List<JxmdSale>> listAll() {
        List<JxmdSale> customerList = saleService.list();
        return commonResult.success(customerList);
    }

    @ApiOperation("根据编号获取")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public commonResult<commonPage<JxmdSale>> list(@RequestParam(value = "keyword", required = false) String keyword,
                                                   @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<JxmdSale> saleList = saleService.list(keyword, pageSize, pageNum);
        return commonResult.success(commonPage.restPage(saleList));
    }

    @ApiOperation("获取销售编号和销售客户")
    @RequestMapping(value = "/getNumberAndCustomer", method = RequestMethod.GET)
    @ResponseBody
    public commonResult<List> getNumberAndCustomer(){
        List  list =  saleService.getNumberAndCustomer();
        if (list !=null){
            return  commonResult.success(list);
        }
        return  commonResult.failed("数据为空");
    }

    /**
     *
     * @param depositoryName  仓库
     * @param saleList        销售订单
     * @return
     */
        @ApiOperation("销售-  出库")
        @RequestMapping(value = "/outSale")
        @ResponseBody
    public  commonResult outSale(@RequestParam(value = "depositoryName",required = false) String depositoryName ,
                                 @RequestBody JxmdSale saleList){
            Map<String, Object> map = new HashMap<>();
            map.put("depositoryName",depositoryName);
            map.put("saleList",saleList);
         int  result = saleService.outSaleAndCheck(map);
            /**
             *  定义接口
             *  0：成功
             *  失败 ！=服务器错误
             *  1： 库存不足，请重新选择出库仓库
             *  2： 不能重复出库
             */
            if (result ==1){
               return commonResult.failed(ResultCode.LACK_STOCK);
            } else if (result ==2) {
                return commonResult.failed(ResultCode.OVER_EXIT_SALE);
            }
            return commonResult.success(ResultCode.SUCCESS);
        }

    /**
     *  回收
     * @param remark   回收原因
     * @param saleList  销售列表
     * @return
     */
    @ApiOperation("回收")
    @RequestMapping(value = "/checkInGoods")
    @ResponseBody
        public  commonResult checkInGoods(@RequestParam(value = "remark",required = false) String remark,
                                          @RequestBody JxmdSale saleList){
            Map<String, Object> map = new HashMap<>();
            map.put("remark",remark);
            map.put("saleList",saleList);
         int  result   = saleService.checkInGoods(map);
        /**
         * 0 操作成功
         * 失败
         * 1 ：已生成销售退货单
         * 2: 原出库清单不存在，回收异常
         */
        if (result ==1){
            return  commonResult.failed(ResultCode.CREATE_S);
        }else if (result ==2){
            return  commonResult.failed(ResultCode.NOT_EXIT);
        }
        return  commonResult.success(ResultCode.SUCCESS);
        }
}
